*This file replicates the Tables and Figures in Limbocker, Resh, and Selin. (202X), which appears in JPART.
*The basic description of the data collection is decribed below, but the file will not
*execute the basic data cleaning and collecting that went into the paper. This is because 
*the total file size of the orignal data for this exceeds 30GB. Instead, we provide the final file, which will 
*produce all the figures and charts from the supporting data. The raw data for the administrative
*records can be found at: https://archive.org/details/opm-federal-employment-data


*First I create a panel of all legal and human resource personnel.
*Legal personnel are 09XX series in the OPM Guide to Data Standards
*HR personnel are 02XX series in the OPM Guide to Data Standards
*For every quarter of data, I load the status file for all of government, keep the revlevant
*presonnel by occupation code, and then save a temporary file to the desktop. I then repeat 
*this process for all quarters from 1973-2014 with the following code:

/*
clear
infile using "C:\Users\Scott.Limbocker\Dropbox\Data\OPM Buzzfeed\opmold.dct",  using("C:\Users\Scott.Limbocker\Documents\Status_Non_DoD_2014_06.txt") 
keep if occupation == "0201" |occupation == "0203" |occupation == "0241" |occupation == "0243" | occupation == "0244"  | occupation =="0260"|  occupation =="0260"| occupation == "0501" |occupation == "0503"|occupation == "0505"|occupation == "0510"|occupation == "0511"| |occupation == "0512"|occupation == "0525"|occupation == "0526"|occupation == "0530"|occupation == "0540" |occupation == "0544"|occupation == "0545"|occupation == "0560"|occupation == "0561"|occupation == "0570" |occupation == "0570"|occupation == "0592"|occupation == "0593"|occupation == "0599" |occupation == "0901"|occupation == "0904"|occupation == "0905"|occupation == "0930"|occupation == "0935" |occupation == "0950"|occupation == "0958"|occupation == "0962"|occupation == "0963" |occupation == "1202"|occupation == "1210"|occupation == "1220"|occupation == "1221"|occupation == "1222" |occupation == "1223" |occupation == "1224"|occupation == "1226" |occupation == "1299" |occupation == "1801" |occupation == "1802"|occupation == "1805" |occupation == "1810"|occupation == "1811" |occupation == "1815"|occupation == "1822"|occupation == "1825"|occupation == "1831"|occupation == "1849" |occupation == "1850" |occupation == "1860" |occupation == "1862" |occupation == "1863"|occupation == "1881"  |occupation == "1889" |occupation == "1894" |occupation == "1895" |occupation == "1896" |occupation == "1899"
save "C:\Users\Scott.Limbocker\Desktop\OPM Panels\ALe614.dta", replace

clear
infile using "C:\Users\Scott.Limbocker\Dropbox\Data\OPM Buzzfeed\opmold.dct",  using("C:\Users\Scott.Limbocker\Documents\Status_Non_DoD_2014_03.txt")
keep if occupation == "0201" |occupation == "0203" |occupation == "0241" |occupation == "0243" | occupation == "0244"  | occupation =="0260"| |occupation == "0901"|occupation == "0904"|occupation == "0905"|occupation == "0930"|occupation == "0935" |occupation == "0950"|occupation == "0958"|occupation == "0962"|occupation == "0963" |occupation == "1202"|occupation == "1210"|occupation == "1220"|occupation == "1221"|occupation == "1222" |occupation == "1223" |occupation == "1224"|occupation == "1226" |occupation == "1299" |occupation == "1801" |occupation == "1802"|occupation == "1805" |occupation == "1810"|occupation == "1811" |occupation == "1815"|occupation == "1822"|occupation == "1825"|occupation == "1831"|occupation == "1849" |occupation == "1850" |occupation == "1860" |occupation == "1862" |occupation == "1863"|occupation == "1881"  |occupation == "1889" |occupation == "1894" |occupation == "1895" |occupation == "1896" |occupation == "1899" 
save "C:\Users\Scott.Limbocker\Desktop\OPM Panels\ALe314.dta", replace
*/

*In the third quarter of 2014, OPM changed their file format. The following code will repeat
*the same importing process under this new data format:

/*
clear 
import delimited "C:\Users\Scott.Limbocker\Dropbox\Data\OPM Buzzfeed\Non_DoD_201409.txt", delimiter("|") clear 
rename v3 date

rename v5 bureau
rename v13 occu
rename v18 work_sched
drop v*

**Code to split OPM code from agency name**
split bureau, p(-)
split occu, p(-)
rename bureau1 agency
rename occu1 occupation
keep date agency occupation work_sched
keep if occupation == "0201" |occupation == "0203" |occupation == "0241" |occupation == "0243" | occupation == "0244"  | occupation =="0260"|  |occupation == "0901"|occupation == "0904"|occupation == "0905"|occupation == "0930"|occupation == "0935" |occupation == "0950"|occupation == "0958"|occupation == "0962"|occupation == "0963" |occupation == "1202"|occupation == "1210"|occupation == "1220"|occupation == "1221"|occupation == "1222" |occupation == "1223" |occupation == "1224"|occupation == "1226" |occupation == "1299" |occupation == "1801" |occupation == "1802"|occupation == "1805" |occupation == "1810"|occupation == "1811" |occupation == "1815"|occupation == "1822"|occupation == "1825"|occupation == "1831"|occupation == "1849" |occupation == "1850" |occupation == "1860" |occupation == "1862" |occupation == "1863"|occupation == "1881"  |occupation == "1889" |occupation == "1894" |occupation == "1895" |occupation == "1896" |occupation == "1899" 
save "C:\Users\Scott.Limbocker\Desktop\temp\1409.dta", replace

*/

*Some unquie personnel identifiers appear multiple times in a given quarter. We remove any duplicates.
*Not all occupation codes kept in the process above were used in the analysis. We use only the following codes:
*0935 for ALJs
*0930 for AJs
*0200 for HR, removing interns and other non-executive types
*We then count the number of each type of personnel in the agency for each by year.
*Once counted, we reduce the data from individual to agency averages, removing
*all individual level covariates
*dept_trimyr_alj_count is the agency-year count of ALJs
*dept_trimyr_hearings_count is the agency-year count of AJs
*dept_trimyr_hearings_count is the agency-year HR executive count
*With the agency-level administrative records assembled, we then merge the 
*agency litigation records from West Law using the common DeptID variable

*

/*
Some other variables needed to be generated as well using the following:
tab DeptID, g(ID) creates the agency fixed effects
tab year, g(yr) creates the year fixed effects.
gen js = dept_trimyr_alj_count + dept_trimyr_hearings_count creates the joint measure of both judge types
drop if DeptID ==. removes any rows of data that were note matched 
sort Year
by Year: egen tot_js = sum(dept_trimyr_alj_count + dept_trimyr_hearings_count) sums all judges by year
by Year: egen tot_aljs = sum(dept_trimyr_alj_count) sums all ALJs by year
by Year: egen tot_ajs = sum(dept_trimyr_hearings_count) sums all by year
*/

*We then merge the agency-level administrative records with the agency-level Westlaw data. The Westlaw
*data were collected as described in the paper. That provides two new variables:
*Cases is the number of litigations the agency appeared in during a given year.
*Rules is the number of rulmaking efforts assoicated with the agency in a given year.
*Other variables:
*DeptID is the numerical agency identify
*DepartmentName is the agency name
*Year is the year of the observation

clear

use "C:\Users\scott.limbocker\Dropbox\Research Projects\ALJS\JPART\replication.dta", replace
xtset DeptID Year
*We observe agencies in court beginning in either 1994 or 1995. To give the correlations a consisent
*state time we begin in 1996 becuase we cannot lag 1995 to an unobserved 1994. This is a quirk of the data
*and not a substantive "beginning" of the agency. The results to not depend upon this descion. 

*Fig1
sort Year
twoway 	(line tot_js Year)  ///
		(line tot_aljs Year)  ///
		(line tot_ajs Year) if Year > 1995
*Figure was then edited for cosmetic changes using the graph editor 
		
*Fig2		
twoway 	(line dept_trimyr_alj_count Year if DepartmentName =="Social Security Administration") ///
		(line dept_trimyr_hearings_count Year if DepartmentName =="Department of the Treasury") ///
		(line dept_trimyr_alj_count Year if DepartmentName =="Department of Energy") ///
		(line dept_trimyr_hearings_count Year if DepartmentName =="Department of Veterans Affairs") ///		
		(line dept_trimyr_alj_count Year if DepartmentName =="Department of Labor") ///
		(line dept_trimyr_hearings_count Year if DepartmentName =="Department of Justice") if Year > 1995
*Figure was then edited for cosmetic changes using the graph editor
		
*Fig3
		
twoway 	(line Cases Year if DepartmentName =="Department of Health and Human Services") ///
		(line Cases Year if DepartmentName =="Federal Deposit Insurance Corporation") ///
		(line Cases Year if DepartmentName =="Securities and Exchange Commission") ///
		(line Cases Year if DepartmentName =="Merit Systems Protection Board") if Year > 1995
*Figure was then edited for cosmetic changes using the graph editor

*Table 1
sort DeptID Year
xtset DeptID Year
pwcorr dept_trimyr_alj dept_trimyr_hearings l.Cases l.Rules if LewisSelin == 1 & Year > 1996


/*

pwcorr dept_trimyr_alj dept_trimyr_hearings l.Cases l.Rules if LewisSelin == 1 
             | ALJS		  AJS	   
-------------+---------------------
     L.Cases |   0.3235   0.1571   
     L.Rules |  -0.0240   0.1559    

*/

tab DeptID, g(ID)
tab Year, g(yr)
**********TABLE2**********
eststo clear
reg js l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year >1996, r
eststo
reg dept_trimyr_alj l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year >1996, r
eststo
reg dept_trimyr_hearing l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year >1996, r
eststo 
esttab, compress drop (ID* yr*) se
*Values may be off in the last digit due to rounding

**********TABLE3**********
eststo clear
reg dept_trimyr_alj l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year >1996 & DepartmentName !="Social Security Administration", r
eststo
reg dept_trimyr_hearing l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year >1996 & DepartmentName !="Social Security Administration", r
eststo
reg dept_trimyr_exec_count l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year >1996, r
eststo

esttab, compress drop (ID* yr*) se
*Values may be off in the last digit due to rounding
*******Appendix figures
*for Table A1
summ js dept_trimyr_alj_count dept_trimyr_hearings_count dept_trimyr_exec_count Cases Rules if LewisSelin == 1 & Year > 1995
*In the paper we report descriptive statistics for all the data. Because we don't differentiate the lag values in the paper,
*we have to change the date restriction to include the 1995 lagged observations in the descriptive statistics reported here.
*This makes the values produced just slightly different 

*Makes Tables A2 and A3
*for Table A2
eststo clear
poisson js l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year > 1996, r difficult
eststo
poisson dept_trimyr_alj l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year > 1996, r difficult
eststo
poisson dept_trimyr_hearing l.Cases l.Rules yr* if LewisSelin == 1 & Year > 1996, r difficult
eststo 

esttab, compress drop (ID* yr*)
*Some coeficients might be differnt due to rounding

*for Table A3
eststo clear
nbreg js l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year > 1996, r difficult
eststo
nbreg dept_trimyr_alj l.Cases l.Rules ID* yr* if LewisSelin == 1 & Year > 1996, r difficult
eststo
nbreg dept_trimyr_hearing l.Cases l.Rules yr* if LewisSelin == 1 & Year > 1996, r difficult
eststo 

esttab, compress drop (ID* yr*)
*Some coeficients might be differnt due to rounding
****Check for causality
*The code below was used to check for causality referenced in the paper. We report none of these tables.
eststo clear
reg Cases l.dept_trimyr_alj l.Rules  ID* yr* if LewisSelin == 1, r
eststo
reg Cases l.dept_trimyr_hearing l.Rules ID* yr* if LewisSelin == 1, r
eststo 
reg Cases l.dept_trimyr_exec_count l.Rules ID* yr* if LewisSelin == 1, r
eststo
esttab, compress drop (ID* yr*)

reg Rules l.dept_trimyr_alj l.Cases  ID* yr* if LewisSelin == 1, r
eststo
reg Rules l.dept_trimyr_hearing l.Cases ID* yr* if LewisSelin == 1, r
eststo 
reg Rules l.dept_trimyr_exec_count l.Cases ID* yr* if LewisSelin == 1, r
eststo
esttab, compress drop (ID* yr*)

*installed pvar from package st0455
sort DeptID Year
pvar js Cases Rules if LewisSelin == 1


gen aljss = dept_trimyr_alj
gen ajss = dept_trimyr_hearing
gen exss = dept_trimyr_exec_count

pvar js Cases Rules if LewisSelin == 1
pvargranger

pvar aljss Rules if LewisSelin == 1
pvargranger

pvar ajss Cases Rules if LewisSelin == 1
pvargranger
 
pvar exss Cases Rules if LewisSelin == 1
pvargranger


*xtbalance, range (1997, 2015) miss (Year DeptID)
*xtgcause js Cases , lags(1)		
